#!/usr/bin/python2.4
#
# Copyright 2008 Google Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


"""Unittests for ranking."""

__author__ = 'stratus@google.com (Gustavo Franco)'

import operator
import unittest
import ranking


class RankingTest(unittest.TestCase):
  """Test the Ranking module."""

  def testSortByExactMatch(self):
    exactlist = [{'name': 'python'}, {'name': 'python2.5'},
                 {'name': 'python3.0'}]

    rank = ranking.Ranking('python2.5')
    exactlist.sort(cmp=rank.SortByExactmatch,
                   key=operator.itemgetter('name'))
    self.assertEqual([{'name': 'python2.5'}, {'name': 'python'},
                      {'name': 'python3.0'}], exactlist)

  def testSortBySubstring(self):
    substringlist = [{'name': 'apache'}, {'name': 'python2.5'},
                     {'name': 'dput'}]

    rank = ranking.Ranking('python')
    substringlist.sort(cmp=rank.SortBySubstring,
                       key=operator.itemgetter('name'))
    self.assertEqual([{'name': 'python2.5'}, {'name': 'dput'},
                      {'name': 'apache'}], substringlist)

  def testSortbyVotes(self):
    voteslist = [{'name': 'python', 'votes': 1},
                 {'name': 'python2.5', 'votes': 2},
                 {'name': 'python3.0', 'votes': 5}]

    rank = ranking.Ranking('python2.5')
    voteslist.sort(cmp=rank.SortByVotes,
                   key=operator.itemgetter('votes'))
    self.assertEqual([{'name': 'python3.0', 'votes': 5},
                      {'name': 'python2.5', 'votes': 2},
                      {'name': 'python', 'votes': 1}], voteslist)


if __name__ == '__main__':
  unittest.main()
